# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def reverseBetween(self, head: ListNode, left: int, right: int) -> ListNode:
        dummy=ListNode(0,head)
        if not head.next:
            return head
        if left==right:
            return head
        head=dummy
        i=1
        while i<left:
            head=head.next
            i+=1
        go=head.next
        s=go
        temp1=go.next.next
        temp2=go.next
        go.next.next=go
        go=temp2
        while i<right-1:
            temp2=temp1.next
            temp1.next=go
            go=temp1
            temp1=temp2
            i+=1
        head.next=go
        s.next=temp1
        return dummy.next


'''
执行用时：
36 ms
, 在所有 Python3 提交中击败了
82.80%
的用户
内存消耗：
15 MB
, 在所有 Python3 提交中击败了
45.64%
的用户
'''